package com.example.libraryapp.activity;

import android.os.Bundle;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;


import com.example.libraryapp.R;
import com.example.libraryapp.fragment.category.CategoryFragment;
import com.example.libraryapp.fragment.bookrack.BookrackFragment;
import com.example.libraryapp.fragment.home.HomeFragment;
import com.example.libraryapp.fragment.MineFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends AppCompatActivity {

    private HomeFragment homeFragment;

    private BookrackFragment bookrackFragment;

    private CategoryFragment categoryFragment;

    private MineFragment mineFragment;

    private Fragment[] fragments;

    private int lastFragment = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        homeFragment = new HomeFragment();
        categoryFragment = new CategoryFragment();
        bookrackFragment = new BookrackFragment();
        mineFragment = new MineFragment();
        fragments = new Fragment[]{homeFragment, bookrackFragment, categoryFragment, mineFragment};
        //设置fragment到布局
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, homeFragment).show(homeFragment).commit();

        BottomNavigationView bottomNavigation = findViewById(R.id.bottomNavigation);
        bottomNavigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    }

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.menu_home:
                    if (lastFragment != 0) {
                        switchFragment(0);
                        lastFragment = 0;
                    }
                    return true;
                case R.id.menu_bookrack:
                    if (lastFragment != 1) {
                        switchFragment(1);
                        lastFragment = 1;
                    }
                    return true;
                case R.id.menu_category:
                    if (lastFragment != 2) {
                        switchFragment(2);
                        lastFragment = 2;
                    }
                    return true;
                case R.id.menu_mine:
                    if (lastFragment != 3) {
                        switchFragment(3);
                        lastFragment = 3;
                    }
                    return true;
                default:
                    break;
            }
            return false;
        }
    };

    /**
     *切换fragment
     */
    private void switchFragment(int index) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        //隐藏上个Fragment
        transaction.hide(fragments[lastFragment]);
        if (!fragments[index].isAdded()) {
            transaction.add(R.id.fragment_container, fragments[index]);
        }
        transaction.show(fragments[index]).commitAllowingStateLoss();
    }

}
